<?php

namespace Api\Controller;

use Home\Logic\GoodsLogic;

use Common\Logic\GroupbuyLogic;
use Common\Logic\ExchangebuyLogic;
use Think\Page;

class ActivityController extends BaseController
{
    protected $logic;

    public function  __construct()
    {
        parent::__construct();

        $this->logic = new GroupbuyLogic;
    }



    /**
     * 获取商品团购列表
     */
    public function info()
    {
        $shop_id = I("shop_id",0);

        try{
            if( $shop_id )
            {
                $res = GroupbuyLogic::getGoodsList([
                    "shop_id" => $shop_id
                ]);
            }else{
                $res = GroupbuyLogic::getGoodsList();
            }
            
            $list = $res['list'];

        }catch(\Exception $e)
        {
            $this->returnInfo('-1','没有团购商品',[]);
        } 

        $this->returnInfo('1','获取成功',$list);
    }
    /**
     * 团购商品详情页
     */
    public function group(){
        
        try{
            $res = GroupbuyLogic::getDetailForGoods([
                "groupbuy_id" => I("groupbuy_id"),
                "user_id" => $this->user_id
            ]);
        }catch(\Exception $e)
        { 
            $this->returnInfo($e->getCode(), $e->getMessage(),[]);
        }

        $this->returnInfo(1, "获取成功", $res );
    }

    /**
     * 获取我的团购信息
     */
    public function getMyGroup(){
        try{
            #默认为待成团
            $group_status = I("group_status",0);
            $res = GroupbuyLogic::getGroupItem([
                "user_id" => $this->user_id
            ],$group_status);
        }catch(\Exception $e)
        { 
            $this->returnInfo($e->getCode(), $e->getMessage(),[]);
        }

        $this->returnInfo(1, "获取成功", $res );
    }


    /**
     * 获取商品兑换列表
     */
    public function exchange()
    {
        $shop_id = I("shop_id",0);

        try{
            if( $shop_id )
            {
                $res = ExchangebuyLogic::getGoodsList([
                    "shop_id" => $shop_id
                ]);
            }else{
                $res = ExchangebuyLogic::getGoodsList();
            } 
        }catch(\Exception $e)
        {
            $this->returnInfo('-1','没有兑换商品',[]);
        } 
        $res['available_points'] = 0;
        if( $this->user_id )
        {
            $res['available_points'] = M('users')->where([
                'user_id' => $this->user_id
            ])->getField('pay_points');
        }
        
        
        $this->returnInfo('1','获取成功',$res);
    }
    /**
     * 兑换商品详情页
     */
    public function exchangeInfo(){
        
        try{
            $res = ExchangebuyLogic::getDetailForGoods([
                "goods_id" => I("goods_id"),
                "user_id" => $this->user_id
            ]);
        }catch(\Exception $e)
        { 
            $this->returnInfo($e->getCode(), $e->getMessage(),[]);
        }

        $this->returnInfo(1, "获取成功", $res );
    }

    // 兑换商品预下单
    public function exchange_prebuy()
    {
        $goods_id = I('goods_id',0);
        $goods_spec = I('goods_spec','');
        $goods_num = I('goods_num',1);
        try {
            $res = \Common\Logic\ExchangebuyLogic::preCreateOrder([
                "user_id" => $this->user_id,
                "goods_id" => $goods_id,
                "spec_key" => $goods_spec,
                "num" => $goods_num
            ]);
        } catch (\Exception $e) {
            $log = "\n".$e->getCode();
            $log .= "\n".$e->getMessage();
            $log .= "\n".$e->getFile();
            $log .= "\n".$e->getLine();
            $log .= "\n".$e->getTraceAsString(); 
            doLog('[ERROR]'.$log,'exchangebuy');
            $this->returnInfo($e->getCode(), $e->getMessage(), "");
        }

        doLog('[result]'.json_encode($res),'exchangebuy');

        $this->returnInfo(1, "获取成功", $res);
    }

    // 兑换商品确认订单
    public function exchange_confirmbuy()
    {
        $goods_id = I('goods_id',0);
        $goods_spec = I('goods_spec','');
        $remark = I('user_note','');
        $address_id = I('address_id',0);
        $goods_num = I('goods_num',1);
        try {
            $res = \Common\Logic\ExchangebuyLogic::createOrder([
                "user_id" => $this->user_id,
                "goods_id" => $goods_id,
                "spec_key" => $goods_spec,
                "num" => $goods_num,
                "address_id" => $address_id,
                "user_note" => $remark,
            ]);;
        } catch (\Exception $e) {
            $this->returnInfo($e->getCode(), $e->getMessage(), "");
        }

        $this->returnInfo(1, "兑换成功", $res);
    }

}